package com.session06.am.cainiao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StaffService extends JFrame {

    private JPanel p1, p2;
    private JLabel area;
    private JTextField txtInput;
    private JButton btn1, btn2;
    String oldName = "";
    String old = "";

    public StaffService(Login login) {

        Staff[] staff = login.getStaff();
        Express[] expresses = login.getExpresses();

        oldName = new String(login.getTxtName().getText());
        old = new String(login.getTxtPwd().getPassword());

        WareHouse wareHouse = new WareHouse();

        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String txtNum = txtInput.getText();
                switch (txtNum) {
                    case "1":
                        pickInto(expresses);
                        txtInput.setText("");
                        break;
                    case "2":
                        pickup(expresses);
                        txtInput.setText("");
                        break;
                    case "3":
                        notTaken(expresses);
                        txtInput.setText("");
                        break;
                    case "4":
                        findCode(expresses);
                        txtInput.setText("");
                        break;
                    case "5":
                        findName(expresses);
                        txtInput.setText("");
                        break;
                    case "6":
                        changPsw(staff);
                        txtInput.setText("");
                        break;
                    case "7":
                        wareHouse.dispose();
                        txtInput.setText("");
                        Welcome welcome = new Welcome();
                }
            }
        });
    }

    private void pickInto(Express[] expresses) {
        String code = JOptionPane.showInputDialog(null, "请输入取货码");
        String consignee = JOptionPane.showInputDialog(null, "请输入收货人");
        String phone = JOptionPane.showInputDialog(null, "请输入收货人电话");

        int x = -1;
        for (int i = 0; i < expresses.length; i++) {
            if (expresses[i] == null) {
                x = i;
                break;
            }
        }
        if (x != -1) {
            expresses[x] = new Express(code, consignee, phone, "2022-05-31", "未取货", "无", "无");
            JOptionPane.showMessageDialog(null, "添加成功");
        }
    }

    private void pickup(Express[] expresses) {
        String code = JOptionPane.showInputDialog(null, "请输入取货码");
        int num = -1;
        for (int i = 0; i < expresses.length; i++) {
            if (expresses[i] != null && code.equals(expresses[i].getPickupCode())) {
                num = i;
                break;
            }
        }

        if (num == -1) {
            JOptionPane.showMessageDialog(null, "该取货码不存在");
        } else {

            if ("未取货".equals(expresses[num].getPickupTime())) {
                String name = JOptionPane.showInputDialog(null, "取件人签字");
                expresses[num].setPickupName(name);
                expresses[num].setPickupTime("2022-05-31");
                expresses[num].setHandler(oldName);
                System.out.println(oldName);
            } else {
                JOptionPane.showMessageDialog(null, "该订单已被取走");
            }
        }
    }

    private void notTaken(Express[] expresses) {
        String s = "取货码   收货人   收货人电话   入库时间   取货时间   取货人   经办人\n";
        int len = 0;
        for (int i = 0; i < expresses.length; i++) {
            if (expresses[i] != null) {
                len++;
            }
        }
        for (int i = 0; i < len; i++) {
            if (expresses[i].getPickupTime().equals("未取货")) {
                s += expresses[i].getPickupCode() + "      " + expresses[i].getConsignee() + "      " + expresses[i].getConsigneePhone() + "      " + expresses[i].getPickInToTime() + "      " +
                        expresses[i].getPickupTime() + "      " + expresses[i].getPickupName() + "      " + expresses[i].getHandler() + "\n";
            }
        }
        JOptionPane.showMessageDialog(null, s);
    }

    private void findCode(Express[] expresses) {
        String code = JOptionPane.showInputDialog(null, "请输入取货码");
        int num = -1;
        for (int i = 0; i < expresses.length; i++) {
            if (expresses[i] != null && code.equals(expresses[i].getPickupCode())) {
                num = i;
                break;
            }
        }

        if (num != -1) {
            String s = "取货码   收货人   收货人电话   入库时间   取货时间   取货人   经办人\n";
            s += expresses[num].getPickupCode() + "   " + expresses[num].getConsignee() + "   " + expresses[num].getConsigneePhone() + "   " + expresses[num].getPickInToTime() + "   " +
                    expresses[num].getPickupTime() + "   " + expresses[num].getPickupName() + "   " + expresses[num].getHandler() + "\n";
            JOptionPane.showMessageDialog(null, s);
        }
    }

    private void findName(Express[] expresses) {
        String name = JOptionPane.showInputDialog(null, "请输入收货人姓名");
        int num = -1;
        for (int i = 0; i < expresses.length; i++) {
            if (expresses[i] != null && name.equals(expresses[i].getConsignee())) {
                num = i;
                break;
            }
        }

        if (num != -1) {
            String s = "取货码   收货人   收货人电话   入库时间   取货时间   取货人   经办人\n";
            s += expresses[num].getPickupCode() + "   " + expresses[num].getConsignee() + "   " + expresses[num].getConsigneePhone() + "   " + expresses[num].getPickInToTime() + "   " +
                    expresses[num].getPickupTime() + "   " + expresses[num].getPickupName() + "   " + expresses[num].getHandler() + "\n";
            JOptionPane.showMessageDialog(null, s);
        }
    }

    private void changPsw(Staff[] staff) {

    }

    public class WareHouse extends JFrame {
        public WareHouse() {
            this.setTitle("员工界面");
            this.setBounds(300, 100, 350, 250);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            p1 = new JPanel();
            area = new JLabel("<html>欢迎使用员工管理<br><br>" +
                    "1、入库<br>2、客户取货<br>3、查看未收货订单<br>4、按取货码查询订单<br>5、按姓名查询订单<br>6、修改密码<br>7、安全退出</html>");
            p1.add(area);
            txtInput = new JTextField();
            txtInput.setPreferredSize(new Dimension(220, 20));
            p1.add(txtInput);
            this.add(p1);

            p2 = new JPanel();
            btn1 = new JButton("确认");
            btn2 = new JButton("取消");
            p2.add(btn1);
            p2.add(btn2);
            this.add(p2, BorderLayout.SOUTH);

            this.setVisible(true);
        }
    }
}
